package com.buli.blmall.admin.common.controller;

import cn.hutool.core.util.IdUtil;
import com.buli.blmall.admin.common.response.Result;
import com.buli.blmall.admin.common.service.CaptchaService;
import com.buli.blmall.admin.consts.ConfigKey;
import com.buli.blmall.admin.system.service.SysConfigService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 验证码控制类
 * @author xiang.gao
 * @date 2024/12/18 19:19
 */
@Slf4j
@RestController
@RequestMapping("/captcha")
@AllArgsConstructor
public class CaptchaController {

    private final CaptchaService captchaService;
    private final SysConfigService sysConfigService;

    /**
     * 是否开启验证码
     * @return
     */
    @GetMapping("/need")
    public Result need() {
        String captchaEnabled = sysConfigService.selectConfigByKey(ConfigKey.SYSTEM_CONFIG_KEY_CAPTCHA);
        return Result.success(Boolean.parseBoolean(captchaEnabled));
    }

    /**
     * 图形验证码 返回base64
     * @return code
     */
    @GetMapping("/code")
    public Result code() {
        String uuid = IdUtil.simpleUUID();
        String imageCode = captchaService.genCode(uuid);
        return Result.success(Map.of("uuid", uuid, "image", imageCode));
    }

    /**
     * 加减乘除验证码
     */
    @GetMapping("/image/match")
    public Result match() {
        String uuid = IdUtil.simpleUUID();
        String matchCode = captchaService.genMatchCode(uuid);
        return Result.success(Map.of("uuid", uuid, "image", matchCode));
    }

}
